package com.nl.controller.xuyi.controller;

import com.alibaba.fastjson.JSON;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.BaseResult;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.xuyi.bean.XuYiOperatorInfo;
import com.nl.controller.xuyi.bean.pocbo.*;
import com.nl.controller.xuyi.service.XuYiPocService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @desc poc平台能力接口
 * @return null
 * @author ll
 * @date 2020/9/24 17:26
 */
@RestController
@RequestMapping("xuYiPoc")
@Slf4j
@Validated
public class XuYiPocController {
    @Autowired
    XuYiPocService xuYiPocService;
    /**
     * @desc 获取运维人员列表
     * @param qryYunWeiListReq
     * @param bindingResult
     * @return com.nl.controller.common.model.BaseResult<java.util.List<com.nl.controller.xuyi.bean.pocbo.QryYunWeiListDispatcher>>
     * @author ll
     * @date 2020/9/24 19:37
     */
    @PostMapping("qryYunWeiList")
    public BaseResult<List<QryYunWeiListDispatcher>> qryYunWeiList(
            @Validated @RequestBody QryYunWeiListReq qryYunWeiListReq,BindingResult bindingResult
            ){
        if(bindingResult.hasErrors()) {
            throw new SmsException(bindingResult.getFieldError().getDefaultMessage());
        }
        log.info("XuYiPocController qryYunWeiList 运维人员查询 start request: "+ qryYunWeiListReq.getName());
        BaseResult<List<QryYunWeiListDispatcher>> yunWeiList =  xuYiPocService.qryYunWeiList2(qryYunWeiListReq.getName());
        log.info("XuYiPocController qryYunWeiList 运维人员查询 end response: "+ JSON.toJSONString(yunWeiList));
        return yunWeiList;
    }

    /**
     * @desc 查询运维人员当前位置信息
     * @param locationRequest
 * @param bindingResult
     * @return com.nl.controller.common.model.BaseResult<java.util.List<com.nl.controller.xuyi.bean.pocbo.LocationInfoBo>>
     * @author ll
     * @date 2020/9/24 17:27
     */
    @PostMapping("qryNowLocation")
    public BaseResult<List<LocationInfoBo>> qryNowLocation(
            @Validated @RequestBody LocationRequest locationRequest, BindingResult bindingResult){
        if(bindingResult.hasErrors()) {
            throw new SmsException(bindingResult.getFieldError().getDefaultMessage());
        }
        if(!StringUtils.isEmpty(locationRequest.getUids())) {
            List<String> uidList = JSON.parseArray(locationRequest.getUids(), String.class);
            locationRequest.setUidList(uidList);
        }
        log.info("XuYiPocController qryNowLocation 获取运维人员当前位置信息 start requeset: "+ JSON.toJSONString(locationRequest));
        BaseResult<List<LocationInfoBo>> nowLocationInfo =  xuYiPocService.qryNowLocation(locationRequest);
        log.info("XuYiPocController qryNowLocation 获取运维人员当前位置信息 end response: "+ JSON.toJSONString(nowLocationInfo));

        return nowLocationInfo;
    }

    /**
     * @desc 获取运维人员历史轨迹信息
     * @param locationRequest
     * @param bindingResult
     * @return com.nl.controller.common.model.BaseResult<java.util.Map<java.lang.String,java.util.List<com.nl.controller.xuyi.bean.pocbo.LocationInfoBo>>>
     * @author ll
     * @date 2020/9/24 17:28
     */
    @PostMapping("qryHisLocation")
    public BaseResult<Map<String,List<LocationInfoBo>>> qryHisLocation(
            @Validated @RequestBody LocationRequest locationRequest, BindingResult bindingResult){
        if(bindingResult.hasErrors()) {
            throw new SmsException(bindingResult.getFieldError().getDefaultMessage());
        }
        if(!StringUtils.isEmpty(locationRequest.getUids())) {
            List<String> uidList = JSON.parseArray(locationRequest.getUids(), String.class);
            locationRequest.setUidList(uidList);
        }
        log.info("XuYiPocController qryHisLocation 获取运维人员历史轨迹信息 start requeset: "+ JSON.toJSONString(locationRequest));
        BaseResult<Map<String,List<LocationInfoBo>>> hisLocationInfo =  xuYiPocService.qryHisLocation(locationRequest);
        log.info("XuYiPocController qryHisLocation 获取运维人员历史轨迹信息 end response: "+ JSON.toJSONString(hisLocationInfo));
        return hisLocationInfo;
    }

    /**
     * @desc 获取范围内运维人员列表以及位置信息
     * @param circleInfo
     * @param bindingResult
     * @return com.nl.controller.common.model.BaseResult<java.util.List<com.nl.controller.xuyi.bean.pocbo.QryYunWeiListDispatcher>>
     * @author ll
     * @date 2020/9/24 17:28
     */
    @PostMapping("qryYunWeiByLocation")
    public BaseResult<List<QryYunWeiListDispatcher>> qryYunWeiByLocation(
            @Validated @RequestBody CircleInfo circleInfo, BindingResult bindingResult){
        if(bindingResult.hasErrors()) {
            throw new SmsException(bindingResult.getFieldError().getDefaultMessage());
        }
        log.info("XuYiPocController qryYunWeiByLocation 根据范围查询运维人员当前位置信息 start requeset: "+ JSON.toJSONString(circleInfo));
        BaseResult<List<QryYunWeiListDispatcher>> yunWeiList =  xuYiPocService.qryYunWeiByLocation(circleInfo);
        log.info("XuYiPocController qryYunWeiByLocation 根据范围查询运维人员当前位置信息 end response: "+ JSON.toJSONString(yunWeiList));

        return yunWeiList;
    }

}
